<?php
/**
 * Created by PhpStorm.
 * User: Jmashimaro
 * Date: 2015/2/4
 * Time: 17:45
 */
class StoreApp extends StorebaseApp {

    /**
     * 店铺信息详情
     */
    public function index() {
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }

        $store_id = $this->get_gp('store_id', 'G');
        $offset = $this->get_gp('offset', 'G');
        $size = $this->get_gp('size', 'G');
        $store_id = $store_id ? intval($store_id) : 0;
        $offset = $offset ? intval($offset) : 0;
        $size = $size ? intval($size) : 10;
        if (!$store_id) {
            $this->ajax_return(FLAG_ERROR, '错误的store_id!');
        }

        /* 店铺信息 */
        $model = m('store');
        $sql = "SELECT store_id,store_name,store_logo,iphone_banner,description FROM ".DB_PREFIX."store WHERE store_id={$store_id}";
        $data['store'] = $model->getRow($sql);
        if (!$data['store']) {
            $this->ajax_return(FLAG_ERROR, '店铺信息不存在!');
        }
        $data['store']['store_logo'] = $data['store']['store_logo'] ? QINIU_URL.$data['store']['store_logo'] : '';
        $data['store']['iphone_banner'] = $data['store']['iphone_banner'] ? QINIU_URL.$data['store']['iphone_banner'] : '';

        //获取我与商铺是否关注
        $user_id = $this->checkLogin();
        $sql = 'SELECT user_id FROM '.DB_PREFIX."collect WHERE user_id={$user_id} AND type='store' AND item_id={$store_id}";

        $info = $model->getRow($sql);
        if ($info) {
            $data['store']['relation'] = 1;
        } else {
            $data['store']['relation'] = 0;
        }

        //去除店铺描述的标签
        $data["store"]["description"] = strip_tags($data["store"]["description"]);

        //获取商品列表
        $sql = "SELECT goods_id,goods_name,default_image,price FROM ".DB_PREFIX."goods WHERE store_id={$store_id}
                AND closed = 0 AND if_show = 1 AND recommended = 1 AND if_examine = 1 AND if_finance = 1
                ORDER BY goods_id DESC LIMIT {$offset},{$size}";
        $goods = $model->getAll($sql);
        if ($goods) {
            foreach ($goods as $key=>$val) {
                $val['default_image'] = $val['default_image'] ? QINIU_URL.$val['default_image'] : '';
                $goods[$key] = $val;
            }
        }
        $data['recommend'] = $goods;
        $data['goods'] = array();
        $this->ajax_return(FLAG_OK, '', $data);
    }






}